package org.game.db.proxy.bean;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Maps;
import com.sojoys.artifact.tools.ToolMap;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.cfg.RoleCfg;
import org.cfg.cache.DiscreteDataCfgCache;
import org.cfg.cache.RoleCfgCache;
import org.constant.DiscreteDataID;
import org.constant.DiscreteDataKey;
import org.constant.RoleConstant;
import org.game.db.bean.Role;
import org.game.db.cache.ItemCache;
import org.game.db.vo.DressVO;
import org.game.db.vo.RoomVO;
import org.game.log.builder.LogDsp;
import org.game.log.dsp.GoodsDsp;
import org.game.manager.RoleManager;
import org.game.quartz.QuartzUpdater;

import com.sojoys.artifact.build.data.base.BasePojoProxy;

/**
 * @author : DengYing
 * @CreateDate : 2017年11月30日 下午2:27:21
 * @Description ：精灵扩展
 */
public class RoleProxy extends BasePojoProxy<RoleProxy, Role>{

	public RoleCfg getCfg(){
		return RoleCfgCache.me().getById(t.getCid());
	}

	// 装备的时装
	private DressVO dressVO = null;

	private RoomVO roomVO = null;

	/** 效果 */
	private Map<Integer,Integer> effectMap = Maps.newHashMap();

	public RoleCfg getConfig() {
		return RoleCfgCache.me().getById(t.getCid());
	}

	public String toString(){
		return ToStringBuilder.reflectionToString(this);
	}

	public DressVO getDressVO() {
		return dressVO;
	}

	public void setDressVO(DressVO dressVO) {
		this.dressVO = dressVO;
	}


	public boolean updateMood(){
		RoleCfg cfg = getConfig();
		List moodChangeList = cfg.getMoodChange();
		for (Object o : moodChangeList) {
			Map map = (Map) o;
			List<Integer> range = (List<Integer>) map.get("range");
			if (t.getMood()>= range.get(0) && t.getMood() <= range.get(1)){
				int decay = (int) map.get("decay");
				if (decay < 0){
					decay = decay*(100 - getEffectByType(RoleConstant.EFFECT_TYPE_1))/100;
				}
				if (decay != 0){
					RoleManager.me().changeMood(t,decay, LogDsp.newBuilder(GoodsDsp.TIMING_UPDATE_MOOD));
					return true;
				}else{
					return false;
				}
			}
		}
		return false;
	}


	public RoomVO getRoomVO(){
		//没有房间的看板娘都用默认房间
		if (roomVO == null){
			roomVO = (RoomVO) ItemCache.me().getItemCacheProxy().getByPlayerIdCid(t.getPlayerId(), DiscreteDataCfgCache.me().getIntData(DiscreteDataID.ROLE, DiscreteDataKey.DEFAULT_ROOM));
		}
		return roomVO;
	}

    public void setRoomVO(RoomVO room) {
        //默认房间不属于任何看板娘
        if (room != null) {
            if (room.isDefaultRoom()) {
                roomVO = null;
            } else {
                roomVO = room;
            }
        }else{
			roomVO = null;
		}


    }

	public Map<Integer, Integer> getEffectMap() {
		return effectMap;
	}

	public RoleProxy setEffectMap(Map<Integer, Integer> effectMap) {
		this.effectMap = effectMap;
		return this;
	}

	public int getEffectByType(int type){
		return ToolMap.getInt(type,effectMap,0);
	}

	/**
	 * 每小时额外增加心情值
	 */
	public boolean addExtraMood(){
		int mood = getEffectByType(RoleConstant.EFFECT_TYPE_2);
		if (mood > 0){
			RoleManager.me().changeMood(t,mood,LogDsp.newBuilder(GoodsDsp.GM_MAIL));
			t.update();
			return true;
		}else{
			return false;
		}
	}

	public void addTriggerDating(int datingCid){
		if (t.getTriggerDating().contains(datingCid)){
			return;
		}
		t.getTriggerDating().add(datingCid);
		t.update();
	}

	public void addCityDatingCount(){
		t.setTodayCityDatingCount(t.getTodayCityDatingCount()+1);
		t.update();
	}


	/**
	 * 是否好感度临界点
	 */
	public boolean isCriticalPoint(){
		boolean isCriticalPoint = t.isCriticalPoint();
		if (isCriticalPoint){
			RoleCfg cfg =  getCfg();
			int maxFavor = cfg.getFavorLevels()[cfg.getFavorLevels().length-1];
			if (maxFavor == t.getFavor()){
				return false;
			}
		}
		return isCriticalPoint;
	}


	/**
	 * 心情值更新
	 */
	public class MoodUpdater implements QuartzUpdater{

		@Override
		public void enterGameUpdate() {
			updateMood();
		}

		@Override
		public Date getLastUpdateTime() {
			return t.getLastMoodUpdateTime();
		}

		@Override
		public void setLastUpdateTime(Date date) {
			t.setLastMoodUpdateTime(date);
		}

		@Override
		public void update() {
			t.update();
		}
	}

}
